package com.shiliao.framework.ui.dilaog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

import com.shiliao.framework.R;
import com.shiliao.framework.YimeiFrameworkAppliction;


/**
 * 医美Toast.
 *
 * @author yjl
 */
public class YimeiToast extends YimeiBaseDialog {
    private TextView mMsgView; // textView
    private static final int DRAWABLE_PADDING = 10;

    /**
     * 构造函数.
     *
     * @param activity context
     */
    @SuppressLint("NewApi")
    public YimeiToast(Activity activity) {
        super(activity);

        if (null == mWindow) {
            return;
        }

        // IceScreamSandwich以上版本直接调用api，去除dim遮罩
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            mWindow.setDimAmount(0);
            // 低版本去除dim遮罩方法
        } else {
            LayoutParams layoutParams = mWindow.getAttributes();
            layoutParams.dimAmount = 0.0f;
            mWindow.clearFlags(LayoutParams.FLAG_DIM_BEHIND);
            mWindow.setAttributes(layoutParams);
        }

        // toast不获得焦点
        mWindow.setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL,
                LayoutParams.FLAG_NOT_TOUCH_MODAL);

        mWindow.setBackgroundDrawableResource(R.drawable.widget_yimeitoast_background);
        if (getLayoutInflater() == null) {
            return;
        }
        mContainerView = getLayoutInflater().inflate(
                R.layout.widget_yimeitoast, null);

        //外层view
        mContentView = (ViewGroup) mContainerView
                .findViewById(R.id.widget_dialog_content_view);
        // 填充textView
        // mMsgView = (TextView) mContainerView.findViewById(R.id.text_view);
        mMsgView = (TextView) getLayoutInflater().inflate(R.layout.widget_toast_text, null);
        setContentView(mMsgView);
    }

    @Override
    public void show(boolean isAutoDismiss) {
        try {
            setInAnimation(AnimationType.TOAST_FLY_IN_FLY_OUT);
            super.show(isAutoDismiss);
        } catch (Exception e) {
        }
    }

    /**
     * 设置toast内容.
     *
     * @param msg     提示内容
     * @param success 处理是否成功
     */
    public YimeiToast setMsg(String msg, boolean success) {
        Drawable drawable = null;

        if (success) {
            drawable = YimeiFrameworkAppliction.getInstance().getResources().getDrawable(R.mipmap.widget_toast_smile);

        } else {
            drawable = YimeiFrameworkAppliction.getInstance().getResources().getDrawable(R.mipmap.widget_toast_cry);
        }

        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight());

        if (null != mMsgView) {
            mMsgView.setCompoundDrawablePadding(DRAWABLE_PADDING);
            mMsgView.setCompoundDrawables(null, drawable, null, null);
            mMsgView.setText(msg);
        }

        return this;
    }

    /**
     * 设置toast内容.
     *
     * @param msg 提示内容
     */
    public YimeiToast setMsg(String msg) {

        if (null != mMsgView) {
            mMsgView.setCompoundDrawablePadding(0);
            mMsgView.setCompoundDrawables(null, null, null, null);
            mMsgView.setText(msg);
        }
        return this;
    }


}
